package com.huanuo.internetofthings.controller.impact;

import com.github.pagehelper.PageInfo;
import com.huanuo.internetofthings.bo.impact.ImpactDeviceInfo;
import com.huanuo.internetofthings.bo.user.UserInfo;
import com.huanuo.internetofthings.dto.impact.ImpactDeviceListInfoRes;
import com.huanuo.internetofthings.dto.statistics.DeviceInfoPageRequestDto;
import com.huanuo.internetofthings.service.impact.ImpactDeviceInfoFacadeService;
import com.huanuo.internetofthings.service.user.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.unicorn.framework.core.ResponseDto;
import org.unicorn.framework.core.SysCode;
import org.unicorn.framework.core.exception.PendingException;

@RestController
@RequestMapping({ "/impact" })
@Api(value = "IMPACT模块", tags = { "IMPACT模块" })
public class ImpactController {

	@Autowired
	private ImpactDeviceInfoFacadeService impactDeviceInfoFacadeService;
	@Autowired
	private UserInfoService userInfoService;

	/**
	 * 用户查询职责范围内设备列表
	 * 
	 * @return
	 * @throws PendingException
	 */
	@ApiOperation("用户查询职责范围内设备列表")
	@RequestMapping(value = { "/page" }, method = { RequestMethod.POST })
	public ResponseDto<PageInfo<ImpactDeviceListInfoRes>> page(
			@RequestBody DeviceInfoPageRequestDto deviceInfoPageRequestDto) throws PendingException {
		// 获得用户信息
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		UserInfo userInfo = checkUser(userIdentity);
		ImpactDeviceInfo impactDeviceInfo = deviceInfoPageRequestDto.getImpactDeviceInfo();
		if (impactDeviceInfo == null) {
			impactDeviceInfo = new ImpactDeviceInfo();
		}
		impactDeviceInfo.setGroupNo(userInfo.getGroupNo());
		deviceInfoPageRequestDto.setImpactDeviceInfo(impactDeviceInfo);
		return new ResponseDto<>(impactDeviceInfoFacadeService.getImpactDeviceInfoPage(deviceInfoPageRequestDto));
	}

	/**
	 * 设备详情
	 * 
	 * @return
	 * @throws PendingException
	 */
	@ApiOperation("设备详情")
	@RequestMapping(value = { "/detail/{id}" }, method = { RequestMethod.GET })
	public ResponseDto<?> detail(@PathVariable("id") Long id) throws PendingException {
		ImpactDeviceInfo impactDeviceInfo = new ImpactDeviceInfo();
		impactDeviceInfo.setId(id);
		return new ResponseDto<>(impactDeviceInfoFacadeService.deviceDetail(impactDeviceInfo));
	}

	/**
	 * 用户查询职责范围内设备在某市分布列表
	 * 
	 * @return
	 * @throws PendingException
	 */
	@ApiOperation("用户查询职责范围内设备在某市分布列表")
	@RequestMapping(value = { "/city/list" }, method = { RequestMethod.POST })
	public ResponseDto<?> cityList(@RequestBody ImpactDeviceInfo impactDeviceInfo ) throws PendingException {
		// 获得用户信息
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		UserInfo userInfo = checkUser(userIdentity);
		impactDeviceInfo.setGroupNo(userInfo.getGroupNo());
		return new ResponseDto<>(impactDeviceInfoFacadeService.getImpactDeviceInfoList(impactDeviceInfo));
	}

	/**
	 * 检查用户
	 * 
	 * @param userIdentity
	 * @return
	 * @throws PendingException
	 */
	public UserInfo checkUser(String userIdentity) throws PendingException {
		// 根据用户标识查询用户信息
		UserInfo userInfo = userInfoService
				.getUserInfoByCondition(UserInfo.builder().userIdentity(userIdentity).build());
		if (userInfo == null) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		if (StringUtils.isBlank(userInfo.getGroupNo())) {
			throw new PendingException(SysCode.SYS_FAIL, "用户不属于任何组");
		}
		return userInfo;
	}
}